<template>
  <div class="system-dic-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-user-search mb15">
        <el-row :gutter="10"> <!-- 行内间距，可根据需求调整 -->
          <el-col :xs="24" :sm="24" :md="8" :lg="6" :xl="6">
            <el-form-item label="关键词"> <!-- 修正 prop 名称，避免与 jId 冲突 -->
              <el-input
                  size="default"
                  placeholder="请输入井名称"
                  style="max-width: 200px"
                  v-model="state.tableData.param.keyword"
              ></el-input>
              <el-button
                  size="default"
                  type="primary"
                  class="ml10"
                  @click="searchRow"
              >
                <el-icon>
                  <ele-Search/>
                </el-icon>
                查询
              </el-button>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="24" :md="8" :lg="6" :xl="6" class="pt5">
            <el-button
                size="default"
                type="success"
                class="ml10"
                @click="onOpenAddJing('add')"
            >
              <el-icon>
                <ele-FolderAdd/>
              </el-icon>
              新增井
            </el-button>
          </el-col>
        </el-row>

      </div>
      <el-table :data="state.tableData.data"
                height="calc(100vh - 300px )"
                :header-cell-style="{ background: '#EEEEEE', }"
                v-loading="state.tableData.loading"
                style="width: 100%"
      >
        <el-table-column prop="id" label="序号" width="80"/>
        <el-table-column prop="name" label="井名称" align="center" show-overflow-tooltip/>
        <el-table-column prop="remake" label="备注" align="center" show-overflow-tooltip/>
        <el-table-column prop="orderNum" label="订单数" align="center" show-overflow-tooltip/>
        <el-table-column prop="unpayOrderNum" label="未支付订单数" align="center" show-overflow-tooltip/>
        <el-table-column prop="workTotalNum" label="累计工作小时数" align="center" show-overflow-tooltip/>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenEditJing('edit', scope.row)">修改</el-button>
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          class="mt15"
          :pager-count="5"
          :page-sizes="[10, 20, 30]"
          v-model:current-page="state.tableData.param.pageNum"
          background
          v-model:page-size="state.tableData.param.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>
    <DicDialog ref="dicDialogRef" @refresh="getTableData()"/>
  </div>
</template>

<script setup lang="ts" name="jd_jing">
import {defineAsyncComponent, reactive, onMounted, ref, nextTick} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import {getJingList, delJing} from '/@/api/jd/jing/index'
// 引入组件
const DicDialog = defineAsyncComponent(() => import('/@/views/jd/jing/addRowRole.vue'));


// 声明 ref 变量引用组件实例
// 定义变量内容
const dicDialogRef = ref();
const lookRoleRef = ref()
const state = reactive<SysDicState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      "keyword": '',
      "pageNum": 1,
      "pageSize": 10
    },
  },
  jdList: []


});

// 初始化表格数据
const getTableData = async () => {
  const data = await getJingList(state.tableData.param);
  state.tableData.loading = true;
  state.tableData.data = data.data.rows;
  state.tableData.total = data.data.total;
  setTimeout(() => {
    state.tableData.loading = false;
  }, 1000);
};

// 新增
const onOpenAddJing = (type: string) => {
  dicDialogRef.value.openDialog(type);
};
// 修改
const onOpenEditJing = (type: string, row: RowDicType) => {
  dicDialogRef.value.openDialog(type, row);
};
// 删除字典
const onRowDel = (row: RowDicType) => {
  ElMessageBox.confirm(`此操作将永久删除记录，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(() => {
        const param = {id: row.id}
        delJing(param)
        setTimeout(() => {
          state.tableData.loading = false;
        }, 1000);
        getTableData();
        ElMessage.success('删除成功');
      })
      .catch(() => {
      });
};
const onDetile = (row: RowDicType) => {
  lookRoleRef.value.openDialog(row)
}
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
const searchRow = () => {
  getTableData();
};
nextTick(() => {
  getTableData();
})
</script>
